<?php

class UserService
{


    /*
     * 通过 mobile 得到 user
     *
     */

    public static function getUserByMobile($mobile)
    {

        $user  = UcUser::model()->find("mobile=:mobile ",
            array(":mobile" => $mobile));

        return $user;

    }

    /*
     *
     * 增加用户
     */
    public static function addUser($mobile,$password)
    {
        $user  = new UcUser();
        $user->mobile = $mobile;
        $user->password = $password;
        $user->user_sign = "";
        $user->deviceInfo = "";
        $user->user_head = "";
        $user->create_time =  DateUtil::GetCurrentUTCDateTime();

        $user->avatar = MsgConst::DEFAULT_AVATAR_URL;

        $r1= $user->save();

        if (!$r1) {

           // print_r($user->errors);

            throw new Exception("用户注册失败");
        }


    }


    /*
     * 通过user_id 获取 user
     *
     */
    public static function getUserByUser_id($user_id)
    {
        $user = UcUser::model()->findByPK($user_id);

        if(empty($user))
            Util::OutputSimpleInJsonFormat(1,"用户不存在");

        return $user;

    }

    /*
     * 模糊 用户数据
     *
     */
    public static function formatUser($user)
    {
        $user->password = "";
        return $user;
    }



    //**********fangwei 添加代码
    /*
     *
     * 修改密码
     */
    public static function changeUserPassword($password,$user)
    {
        $user->password = $password;
        $user->update_time = DateUtil::GetCurrentUTCDateTime();

        $r1= $user->save();

        if (!$r1) {

            print_r($user->errors);

            throw new Exception("修改密码错误");
        }


    }
    /*
     * 判断密码是否合法化
     *
     */
    public static function islegalPasswod($password)
    {

        //英文、数字6-20位字符
       // $preg='/^[a-zA-Z\d_]{6,20}$/';

        $preg='/^[a-zA-Z\d_]{32}$/';

        if(preg_match($preg,$password)==false)
        {
            throw new Exception("密码不合法");
        }

    }

    /*
     * 判断旧密码是否和数据库一致
     *
     */
    public static function isSamePasswod($oldPassword,$user)
    {
        if ($user->password != $oldPassword)//用户旧密码错误
        {
            throw new Exception("用户旧密码错误");
        }


    }
    /*
         * 修改用户信息
         *
         */
    public static function changeUserInfo($user,$real_name,$deviceInfo,$user_head,$sex)
    {


        $user->sex = $sex;
        $user->real_name = $real_name;
        $user->deviceInfo = $deviceInfo;
        $user->user_head = $user_head;
        $user->update_time =  DateUtil::GetCurrentUTCDateTime();



        $r1= $user->save();

        if (!$r1) {

            print_r($user->errors);

            throw new Exception("input into user error");
        }

    }

    /*
     *
     * 手机号验证
     *
     */
    public  static function isVaildMobile($mobile)
    {


        //手机号
        $preg="/^1[34578]\d{9}$/";

        if(preg_match($preg,$mobile)==false)
        {
            throw new Exception("手机号不合法");
        }



    }

    /*
     * 登录
     * cxj
     */
    public static function login($mobile,$pwd)
    {
        $user = UcUser::model()->find("mobile=:mobile",
            array(":mobile" => $mobile));

        //手机号是否存在
        if(empty($user))
        {
            throw new Exception("手机号不存在");
        }
        //密码是否正确
        if(strtolower($user->password) == strtolower($pwd))
        {
            return $user;
        }
        else
            throw new Exception("密码错误");


    }

    /*
     * 手机验证码 登录
     * cxj
     */
    public static function smsLogin($mobile,$msg_code)
    {
        $user = UcUser::model()->find("mobile=:mobile",
            array(":mobile" => $mobile));

        //手机号是否存在
        if(empty($user))
        {
            throw new Exception("手机号不存在");
        }

        //验证码是否正确
        SmsService::isVaildCode($mobile,$msg_code,MsgConst::SMS_TYPE_LOGIN);

        return $user;


    }




}


?>